//
//  PersonViewController.m
//  WhatsChat
//
//  Created by 彭琦峰 on 16/3/23.
//  Copyright © 2016年 tanchen. All rights reserved.
//

#import "PersonViewController.h"
//昵称修改
#import "UserNameViewController.h"
#import <AVOSCloud/AVOSCloud.h>
#import "MyQRViewController.h"

@interface PersonViewController ()<UITableViewDataSource,UITableViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

//表格视图
@property (nonatomic,strong)UITableView *tableView;

//头像
@property (nonatomic,strong)UIImageView *headerImageView;

//获取服务器数据
@property (nonatomic,strong)AVQuery *query;
//昵称
@property (nonatomic,strong)UILabel *label;

@end

@implementation PersonViewController

- (void)viewWillAppear:(BOOL)animated{
    
//    //获取服务器上数据
//    self.query = [AVQuery queryWithClassName:@"UserName"];
//    NSString *currentUsername = [AVUser currentUser].username;// 当前用户名
//    AVObject *object = [self.query getFirstObject];
//    self.label.text = object[[NSString stringWithFormat:@"name%@",currentUsername]];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    //设置字体颜色
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    //设置表格视图
    self.tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
}

// Called when a button is clicked. The view will be automatically dismissed after this call returns

//区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 4;
    }else {
        return 3;
    }
}
//cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    //cell布局
    if (indexPath.section == 0 && indexPath.row == 0) {
        
        cell.textLabel.text = @"头像";
        
        self.headerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.width - 80, 10, 60, 60)];
        
        [cell addSubview:self.headerImageView];
        
    }else if (indexPath.section == 0 && indexPath.row == 1) {
        
        cell.textLabel.text = @"昵称";
        self.label = [[UILabel alloc]initWithFrame:CGRectMake(self.view.width - 200, 5, 180, 30)];
        
//        //获取服务器上数据
//        self.query = [AVQuery queryWithClassName:@"UserName"];
//        NSString *currentUsername = [AVUser currentUser].username;// 当前用户名
//        AVObject *object = [self.query getFirstObject];
//        self.label.text = object[[NSString stringWithFormat:@"name%@",currentUsername]];
        
        //传值(用户名)
        [[NSUserDefaults standardUserDefaults] setObject:self.label.text forKey:@"name"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        
        self.label.alpha = 0.5;
        self.label.textAlignment = NSTextAlignmentRight;
        [cell addSubview:self.label];
        
    }else if (indexPath.section == 0 && indexPath.row == 2) {
        
        cell.textLabel.text = @"微聊号";
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(self.view.width - 200, 5, 180, 30)];
        label.text = @"未设置";
        label.alpha = 0.5;
        
        label.textAlignment = NSTextAlignmentRight;
        
        [cell addSubview:label];
        
    }else if (indexPath.section == 0 && indexPath.row == 3) {
        
        cell.textLabel.text = @"二维码名片";
        
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.width - 60, 5, 30, 30)];
        imageView.image = [UIImage imageNamed:@"iconfont-erweima"];
        
        [cell addSubview:imageView];
    }else if(indexPath.section == 1 && indexPath.row == 0) {
        
        cell.textLabel.text = @"性别";
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(self.view.width - 200, 5, 180, 30)];
        label.text = @"男";
        label.alpha = 0.5;
        label.textAlignment = NSTextAlignmentRight;
        [cell addSubview:label];
        
    }else if(indexPath.section == 1 && indexPath.row == 1) {
    
        cell.textLabel.text = @"地区";
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(self.view.width - 200, 5, 180, 30)];
        label.text = @"中国";
        label.alpha = 0.5;
        label.textAlignment = NSTextAlignmentRight;
        [cell addSubview:label];
        
    }else {
    
        cell.textLabel.text = @"个性签名";
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(self.view.width - 200, 5, 180, 30)];
        label.text = @"世界任我行";
        label.alpha = 0.5;
        label.textAlignment = NSTextAlignmentRight;
        [cell addSubview:label];
        
    }
    
    return cell;
}

//cell的点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0 && indexPath.row == 0) {
    
        [self callActionSheetFunc];
        
    }else if (indexPath.section == 0 && indexPath.row == 1) {
    
        //设置push返回按钮标题
        self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"更改名字" style:UIBarButtonItemStyleDone target:nil action:nil];
        //点击跳转到昵称页面
        UserNameViewController *userVC = [[UserNameViewController alloc]init];
        [self.navigationController pushViewController:userVC animated:YES];
        
    }else if (indexPath.section == 0 && indexPath.row == 3) {
    
        //设置push返回按钮标题
        self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"我的二维码" style:UIBarButtonItemStyleDone target:nil action:nil];
        MyQRViewController *vc = [MyQRViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}

/**
 @ 调用ActionSheet
 */
- (void)callActionSheetFunc{
    
    //警告控制器
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"选择图像" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        //先设定sourceType为相机，然后判断相机是否可用（ipod）没相机，不可用将sourceType设定为相片库
        UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
        if (![UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
            sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        }
        //初始化控制器
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        imagePickerController.delegate = self;
        imagePickerController.allowsEditing = YES;
        //设定sourceType样式
        imagePickerController.sourceType = sourceType;
        imagePickerController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        // 跳转到相机或相册页面
        [self presentViewController:imagePickerController animated:YES completion:^{
        }];
    }];
    
    UIAlertAction *alertAction1 = [UIAlertAction actionWithTitle:@"从相册中选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        imagePickerController.delegate = self;
        imagePickerController.allowsEditing = YES;
        //来源:相册
        imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        imagePickerController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        // 跳转到相机或相册页面
        [self presentViewController:imagePickerController animated:YES completion:^{
        }];
        
    }];
    
    UIAlertAction *alertAction2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    //警告控制器添加点击事件
    [alertVC addAction:alertAction];
    [alertVC addAction:alertAction1];
    [alertVC addAction:alertAction2];
    
    [self presentViewController:alertVC animated:YES completion:nil];
    
}

//获取相册照片代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
    
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    
    self.headerImageView.image = image;
}

//行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0 && indexPath.row == 0) {
        return 80;
    }else {
        return 40;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
